package com.ting.test.algorithms.stack;

/**
 * 利用数组 自定义一个栈
 * 栈:
 * 先进后出 头进头出
 */
public class 自定义栈 {

    public static void main(String[] args) {
        int[] arr = new int[]{11, 2, 4, 6, 8, 9, 0, 6};
        MyStack stack = new MyStack(7);
        stack.pop();
        for(int s:arr){
            boolean push = stack.push(s);
            System.out.println("push:"+s+","+push);
        }

        for(int s:arr){
            Integer push = stack.pop();
            System.out.println(push);
        }
    }

    static class MyStack {
        int[] stack;
        int index;

        public MyStack(int size) {
            stack = new int[size];
            index = -1;
        }

        public boolean push(int param) {
            if (index >= stack.length - 1) {
                return false;
            }
            stack[++index] = param;
            return true;
        }

        public Integer pop() {
            if (index < 0) {
                return null;
            }
            return stack[index--];
        }
    }
}
